package org.mech.sefer.client.ui.component.bibletreeview;

import java.util.List;

import org.apache.pivot.beans.BXML;
import org.apache.pivot.util.ListenerList;
import org.apache.pivot.wtk.Button;
import org.apache.pivot.wtk.Button.State;
import org.apache.pivot.wtk.ButtonGroup;
import org.apache.pivot.wtk.ButtonStateListener;
import org.apache.pivot.wtk.Component;
import org.apache.pivot.wtk.PushButton;
import org.apache.pivot.wtk.TablePane;
import org.mech.sefer.client.i18n.I18n;
import org.mech.sefer.client.i18n.I18nAware;
import org.mech.sefer.client.mvp.wtk.BxmlView;
import org.mech.sefer.client.mvp.wtk.ComponentViewImpl;
import org.mech.sefer.client.ui.component.bibletreeview.BibleTree.Mode;
import org.mech.sefer.model.bible.TBible;
import org.springframework.context.annotation.Scope;

@Scope("prototype")
@BxmlView(useCommonPath = true)
@org.springframework.stereotype.Component("bibleTreeView")
public class BibleTreeView extends ComponentViewImpl implements BibleTreePresenter.View, I18nAware {
	
	@BXML
	private TablePane root;
	
	@BXML
	private BibleTree bibleTree;
	
	@BXML
	private PushButton normalButton;
	
	@BXML
	private PushButton oldNewButton;
	
	
	private ButtonGroup buttonGroup;

	@Override
	public Component asComponent() {
		return root;
	}

	@Override
	public void setData(List<TBible> bibles) {
		bibleTree.setBibles(bibles);
	}
	
	@Override
	protected void onSetup() {
	
		buttonGroup = new ButtonGroup();
		buttonGroup.add(normalButton);
		buttonGroup.add(oldNewButton);
		
		ButtonStateListener buttonStateListener = new ButtonStateListener() {

			@Override
			public void stateChanged(Button button, State state) {
				if (normalButton == button && State.UNSELECTED.equals(state)) {
					bibleTree.setMode(Mode.LIST);
				}
				if (oldNewButton == button
						&& State.UNSELECTED.equals(state)) {
					bibleTree.setMode(Mode.OLD_NEW);
				}
			}
		};
		
		normalButton.getButtonStateListeners().add(buttonStateListener);
		oldNewButton.getButtonStateListeners().add(buttonStateListener);
	}
	
	public ListenerList<ButtonStateListener> getNormalButton(){
		return normalButton.getButtonStateListeners();
	}

	@Override
	public void setI18n(I18n i18n) {
		bibleTree.setI18n(i18n);
		normalButton.setTooltipText(i18n.get(getClass(), "listnormal"));
		oldNewButton.setTooltipText(i18n.get(getClass(), "listoldnew"));
	}
	

}
